//
//  YHInputAlertView.m
//  AddressBookCloud
//
//  Created by 林宁宁 on 2020/4/14.
//  Copyright © 2020 林宁宁. All rights reserved.
//

#import "YHInputAlertView.h"
#import "NSObject+Keyboard.h"
#import "UIView+YHInputAccessory.h"
#import "YHDefines.h"
#import "YHConstant.h"
#import "UIColor+YHStyle.h"
#import "UIFont+YH.h"
#import "YHDeviceInfo.h"

@implementation YHInputAlertView


- (void)buildOtherContentV{
    
    self.contentWidth = [YHDeviceInfo s_width]*0.8;
    self.headTitleAligment = NSTextAlignmentCenter;
    self.titleToTopSpace = Adapted(20);
    self.titleToContentSpace = Adapted(15);
    self.contentToButtonSpace = Adapted(20);
    
    [self.contentHeadTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.width.equalTo(@(self.contentWidth));
        make.centerX.equalTo(self.contentView);
    }];
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 200, 200, 44)];
    self.textField.textAlignment = NSTextAlignmentLeft;
    self.textField.font = [UIFont yh_pflOfSize:14];
    self.textField.textColor = [UIColor yh_h1];
    self.textField.backgroundColor = [[UIColor yh_background] lighterColor:0.9];
    self.textField.keyboardAppearance = UIKeyboardAppearanceDark;
    self.textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    self.textField.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    self.textField.rightViewMode = UITextFieldViewModeAlways;
    self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.textField addTarget:self action:@selector(textFieldTextChange) forControlEvents:UIControlEventEditingChanged];
    [self addCustomView:self.textField];

    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self yh_keyboardShowAtAnimationView:self.contentView];
        
        [self.textField becomeFirstResponder];
        
    });
}

- (void)textFieldTextChange
{
    if([self.textField.text hasPrefix:@"\n"])
    {
        self.textField.text = [self.textField.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        
        [self.textField resignFirstResponder];
        
        return;
    }
    
//    UITextRange *selectedRange = [self.textField markedTextRange];
//    NSString * newText = [self.textField textInRange:selectedRange];
//
//    if(newText.length == 0)
//    {
//        if([self.textField.text length] > self.limitNum)
//        {
//            self.textField.text = [self.textField.text substringToIndex:self.limitNum];
//        }
//    }
}



-(void)yh_popDismissAnimation{
    [self.textField resignFirstResponder];
    [super yh_popDismissAnimation];
}

- (void)yh_popShowAnimation{
    [super yh_popShowAnimation];
    
    if(self.showConfigBlock){
        self.showConfigBlock(self.textField,self);
    }
}


@end
